package cn.jslancer.spider;

import cn.jslancer.domain.Article;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jslancer
 * @date 2017/11/5
 */
@Component
public class ArticleSpider extends Spider<Article> {
    //private String startUrl = "http://www.jianshu.com/trending/weekly?page=1";
    private String startUrl = "http://www.jianshu.com/trending/weekly";

    private String baseUrl = "http://www.jianshu.com";

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd.HH.mm*");

    @Override
    public Article parseData(String url, String response) {
        Document doc = Jsoup.parse(response);

        String title = doc.select("div.article > h1").text();
        String author = doc.select("div.article > div.author > div > span > a").text();
        String date = doc.select("div.article > div.author > div > div > span.publish-time").text();
        String content = doc.select("div.article > div.show-content").text();
        String image = doc.select("div.article > div.show-content > div:nth-child(1) > img").attr("src");

        if (StringUtils.isEmpty(title)) {
            return null;
        }
        Article article = new Article();
        article.setTitle(title);
        article.setUrl(url);
        article.setAuthor(author);
        article.setContent(content);
        article.setImage(image);

        try {
            article.setUpdateTime(dateFormat.parse(date));
            article.setCreateTime(dateFormat.parse(date));
        } catch (ParseException e) {
            article.setCreateTime(new Date());
            article.setUpdateTime(new Date());
        }
        return article;
    }

    @Override
    public List<String> parseUrls(String url, String response) {
        Document doc = Jsoup.parse(response);
        Elements linkElements = doc.select("li[id^=note-] > div > a");
        return linkElements.stream()
                .map(element -> baseUrl + element.attr("href"))
                .collect(Collectors.toList());
    }

    @Override
    public String getStartUrl() {
        return startUrl;
    }
}
